package com.hndzyu.yd.customtask;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.gyf.barlibrary.ImmersionBar;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.hndzyu.yd.R;
import com.hndzyu.yd.bean.MySubTask;
import com.hndzyu.yd.bean.UserBean;
import com.hndzyu.yd.url.Path;
import com.hndzyu.yd.utils.OkHttpDownloadJsonUtil;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by zz9527 on 2018/11/8.
 */
public class ApprovalActivity extends Activity {
    @BindView(R.id.top_back)
    ImageView topBack;
    @BindView(R.id.top_title)
    TextView topTitle;
    @BindView(R.id.recycler)
    RecyclerView recycler;
    @BindView(R.id.srl)
    SmartRefreshLayout srl;
    private MySubTaskAdapter mAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_approval);
        ButterKnife.bind(this);
        ImmersionBar.with(this).fitsSystemWindows(true).statusBarColor(R.color.colorAccent).init();
        initData();
        setListener();
    }

    private void initData() {
        topTitle.setText("我发布的任务");
        recycler.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new MySubTaskAdapter(this);
        recycler.setAdapter(mAdapter);
        screen();
    }

    private void setListener() {
        topBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        srl.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                screen();
            }
        });
        mAdapter.setMySubTaskItemListener(new MySubTaskAdapter.mySubTaskitemListener() {
                                              @Override
                                              public void onItemClick(MySubTask.DataBean item) {
                                                  startActivity(new Intent(ApprovalActivity.this, CreditTaskDetailsActivity.class).putExtra("taskid", item.getTaskid()).putExtra("type", item.getStatus()));
                                              }
                                          }
        );
    }

    private void screen() {
        OkHttpDownloadJsonUtil.downloadJson(ApprovalActivity.this, Path.mysubTaskList + "?uid=" + UserBean.uid, new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {

            @Override
            public void onsendJson(String json) {
                srl.finishRefresh();
                srl.finishLoadMore();
                if (!"1".equals(json) && !"0".equals(json)) {
                    Gson gson = new Gson();
                    MySubTask mySubTask = gson.fromJson(json, MySubTask.class);
                    if (mySubTask.getData().size() > 0) {
                        mAdapter.updateData(mySubTask.getData(), mySubTask.getSys().getVipuser());
                    } else {
                        Toast.makeText(ApprovalActivity.this, "未发布任务", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(ApprovalActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
    }
}
